package by.grsu.transport.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.grsu.transport.model.User;
import by.grsu.transport.service.ServiceManager;
import by.grsu.transport.service.UserService;

public class UserAction  extends Action{
	
	private static final String SUCCESS = "SUCCESS";
	private static final String USERS = "users";
	
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//UserForm userForm = (UserForm) form;
		
		try{
			
			ServiceManager serviceManager = ServiceManager.getDefault();
			UserService userService = serviceManager.getUserService();
			
			List<User> users = userService.getUsers(User.Role.valueOf(request.getParameter("role")));
			request.setAttribute(USERS, users);
			request.setAttribute("role", request.getParameter("role"));
			
			if(request.getParameter("user")!=null){
				userService.getUser(request.getParameter("user"));
				request.setAttribute("user", userService.getUser(request.getParameter("user")));
				
			}
			
		}
		catch(Exception e){
			
			
		}
			
		return mapping.findForward(SUCCESS);
		
	}

}
